<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/factory/CommandFactory.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];
	
	class PresenterEmployee {
		/**
		 * Constructor of the presenter
		 */
		public function __construct() {}
		
		/**
		 * Method to execute to create an employee
		 */
		public function clickCreateEmployee ($employeeForm) {
			$address = explode(", ", $employeeForm['address']);
			$country = $address[3];
			$stateProvince = $address[2];
			$city = $address[1];
			$zone = $address[0];
			
			$phones = explode("\n", $employeeForm['phones']);
			$firstPhone = explode(": ", $phones[0]);
			$homePhone = $firstPhone[1];		
			$secondPhone = explode(": ", $phones[1]);
			$mobilePhone = $secondPhone[1];		
			$thirdPhone = explode(": ", $phones[2]);
			$workPhone = $thirdPhone[1];
			$fourthPhone = explode(": ", $phones[3]);
			$faxPhone = $fourthPhone[1];
			
			$command = CommandFactory :: createCommandCreateEmployee($employeeForm['ci'],
				$employeeForm['firstName'], $employeeForm['secondName'], 
				$employeeForm['firstLastName'], $employeeForm['secondLastName'],
				$employeeForm['emailAddress'], date("Y-m-d", time()), '', $country, 
				$stateProvince, $city, $zone, $homePhone, $mobilePhone, $workPhone, $faxPhone, 
				$employeeForm['employeeStartDate'], $employeeForm['employeeEndDate'], 
				$employeeForm['job'], $employeeForm['salary'], $employeeForm['branch'], 
				$employeeForm['department']);
			$command -> execute();		
			return $command -> getResult();
		
		}
		
		/**
		 * Method to execute to load existing branches
		 */
		public function loadExistingBranches() {
			$command = CommandFactory :: createCommandSelectBranches();
			$command -> execute();
			$branches = $command -> getResult();
			
			$result = '<select name="branch" onchange="xajax_updateDepartmentsList(document.employeeForm.branch.options[document.employeeForm.branch.selectedIndex].value)">';
			$result .= '<option value="0">Seleccione una sucursal</option>';
			$i = 0;
			
			while ($i < count($branches['pk_rif'])) {
				$result .= '<option value="' . $branches['pk_rif'][$i] . '">' . 
					$branches['name'][$i++] . '</option>';
			}
			
			$result .= '</select>';
			
			return $result;	
		}
		
		/**
		 * Method to execute to load existing departments by branch rif
		 */
		public function updateDepartmentsList($rif) {
			$command = CommandFactory :: createCommandSelectDepartmentsByBranchRif($rif);
			$command -> execute();
			$departments = $command -> getResult();
			
			$result = '<select name="department">';
			$result .= '<option value="0">Seleccione un departamento</option>';
			$i = 0;
			
			while ($i < count($departments['pk_id'])) {
				$result .= '<option value="' . $departments['pk_id'][$i] . '">' . 
					$departments['name'][$i++] . '</option>';
			}
			
			$result .= '</select>';
			
			return $result;	
		}
		
		
		
		/**
		 * Method to execute to select Employees
		 */
		public function selectEmployees() {
					$GLOBALS['debug']->log('Inicio funcion select employees---presentador');
			$command = CommandFactory :: createCommandSelectEmployees();
			
			$command -> execute();
			$employees = $command -> getResult();
			
			$result = '<table class="formTable" cellspacing="0" cellpadding="5">';
			$result .= '<tr class="odd">';
			$result .= '<td align="center"><b>Cedula</b></td>';
			$result .= '<td align="center"><b>Primer Nombre</b></td>';
			$result .= '<td align="center"><b>Segundo Nombre</b></td>';
			$result .= '<td align="center"><b>Primer Apellido</b></td>';
			$result .= '<td align="center"><b>Segundo apellido</b></td>';
			$result .= '<td align="center"><b>Correo Electronico</b></td>';
			$result .= '</tr>';
			$i = 0;
			while ($i < count($employees['pk_id_number'])) {
				if ($i % 2 == 0) $result .= '<tr class="even">';
				else $result .= '<tr class="odd">';
				
				$result .= '<td align="center">' . $employees['pk_id_number'][$i] . '</td>';
				$result .= '<td align="center">' . $employees['first_name'][$i] . '</td>';
				$result .= '<td align="center">' . $employees['second_name'][$i] . '</td>';
				$result .= '<td align="center">' . $employees['first_last_name'][$i] . '</td>';
				$result .= '<td align="center">' . $employees['second_last_name'][$i] . '</td>';
				$result .= '<td align="center">' . $employees['email_address'][$i++] . '</td>';
			

				$result .= '</tr>';
			}
			

			$result .= '</table>';
		
			return $result;
		}
		
		
		
		
		
	}
?>